package com.otitan.la.forest.industry.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.la.forest.industry.constant.DicTypeEnum;
import com.otitan.la.forest.industry.mapper.ProjectApplicationMapper;
import com.otitan.la.forest.industry.entity.ProjectApplicationEntity;
import com.otitan.la.forest.industry.dto.ProjectApplicationDTO;
import com.otitan.la.forest.industry.utils.CommonUtils;
import com.otitan.la.forest.industry.vo.ProjectApplicationVO;
import com.otitan.la.forest.industry.service.ProjectApplicationService;
import com.otitan.webapp.framework.basepro.exception.code.BaseResponseCode;
import com.otitan.webapp.framework.basepro.model.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ProjectApplicationServiceImpl extends ServiceImpl<ProjectApplicationMapper, ProjectApplicationEntity> implements ProjectApplicationService {

    @Override
    public DataResult add(ProjectApplicationEntity projectApplication, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }

        LambdaQueryWrapper<ProjectApplicationEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProjectApplicationEntity::getApplicationUnit, projectApplication.getApplicationUnit());
        int count = this.count(queryWrapper);
        if (count > 0){
            DataResult.fail("项目名称不能重复");
        }

        boolean isSuccess = this.save(projectApplication);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult batchDelete(List<String> ids) {
        boolean isSuccess = this.removeByIds(ids);
        log.debug("\n>>>>>>>>> 【删除项目申报】 <<<<<<<<<\n操作结果：{}，ids：{}\n>>>>>>>>> ---- <<<<<<<<<", isSuccess, ids);
        return DataResult.success();
    }

    @Override
    public DataResult editor(ProjectApplicationEntity projectApplication, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }

        LambdaQueryWrapper<ProjectApplicationEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProjectApplicationEntity::getApplicationUnit, projectApplication.getApplicationUnit());
        queryWrapper.ne(ProjectApplicationEntity::getId, projectApplication.getId());
        int count = this.count(queryWrapper);
        if (count > 0){
            return DataResult.fail("项目名称不能重复");
        }

        boolean isSuccess = this.updateById(projectApplication);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult findPageData(long page, long size, ProjectApplicationDTO projectApplication) {
        Page pagePlug = new Page(page, size);
        LambdaQueryWrapper<ProjectApplicationEntity> queryWrapper = Wrappers.lambdaQuery();
        // 申报单位
        if (StrUtil.isNotEmpty(projectApplication.getApplicationUnit())) {
            queryWrapper.like(ProjectApplicationEntity::getApplicationUnit, projectApplication.getApplicationUnit());
        }
        // 项目名称
        if (StrUtil.isNotEmpty(projectApplication.getProjectName())) {
            queryWrapper.like(ProjectApplicationEntity::getProjectName, projectApplication.getProjectName());
        }
        // 开始时间
        if (projectApplication.getApplicationDateStart() != null) {
            queryWrapper.ge(ProjectApplicationEntity::getApplicationDate, projectApplication.getApplicationDateStart());
        }
        // 结束时间
        if (projectApplication.getApplicationDateEnd() != null) {
            queryWrapper.le(ProjectApplicationEntity::getApplicationDate, projectApplication.getApplicationDateEnd());
        }

        IPage<ProjectApplicationEntity> iPage = this.page(pagePlug, queryWrapper);

        IPage<ProjectApplicationVO> iPageVO = new Page<>();
        BeanUtils.copyProperties(iPage, iPageVO);

        List<ProjectApplicationVO> listVO = new ArrayList<>();
        // 转换使用字典的属性
        if (CollUtil.isNotEmpty(iPage.getRecords())) {
            iPage.getRecords().forEach(item -> {
                ProjectApplicationVO vo = new ProjectApplicationVO();
                BeanUtils.copyProperties(item, vo);
                // 需要转换的具体字典属性

                listVO.add(vo);
            });
        }
        iPageVO.setRecords(listVO);

        return DataResult.success(iPageVO);
    }

    @Override
    public DataResult findOne(String id) {
        ProjectApplicationEntity entity = this.getById(id);
        ProjectApplicationVO vo = new ProjectApplicationVO();
        BeanUtils.copyProperties(entity, vo);
        // 需要转换的具体字典属性

        return DataResult.success(vo);
    }
}